using Lzfy_Refund_Service.Models;
using Lzfy_Refund_Service.Repositories;

namespace Lzfy_Refund_Service.Services
{
    /// <summary>
    /// 患者退费密码服务实现
    /// </summary>
    public class PatientRefundPasswordService : IPatientRefundPasswordService
    {
        private readonly IPatientRefundPasswordRepository _repository;
        private readonly ILogger<PatientRefundPasswordService> _logger;

        public PatientRefundPasswordService(
            IPatientRefundPasswordRepository repository,
            ILogger<PatientRefundPasswordService> logger)
        {
            _repository = repository;
            _logger = logger;
        }

        /// <summary>
        /// 更新患者退费密码相关信息
        /// </summary>
        /// <param name="request">更新请求</param>
        /// <returns>更新响应</returns>
        public async Task<PatientRefundPasswordUpdateResponse> UpdatePatientRefundPasswordAsync(PatientRefundPasswordUpdateRequest request)
        {
            try
            {
                _logger.LogInformation("Starting to update patient refund password for PatientId: {PatientId}", request.PatientId);

                // 验证患者是否存在
                var patientExists = await _repository.PatientExistsAsync(request.PatientId);
                if (!patientExists)
                {
                    _logger.LogWarning("Patient not found for PatientId: {PatientId}", request.PatientId);
                    return new PatientRefundPasswordUpdateResponse
                    {
                        Success = false,
                        Message = $"患者ID '{request.PatientId}' 不存在",
                        PatientId = request.PatientId
                    };
                }

                // 执行更新操作
                var modifyTime = await _repository.UpdatePatientRefundPasswordAsync(request);
                
                if (modifyTime.HasValue)
                {
                    _logger.LogInformation("Successfully updated patient refund password for PatientId: {PatientId}", request.PatientId);
                    return new PatientRefundPasswordUpdateResponse
                    {
                        Success = true,
                        Message = "患者退费密码信息更新成功",
                        PatientId = request.PatientId,
                        ModifyTime = modifyTime.Value
                    };
                }
                else
                {
                    _logger.LogWarning("Failed to update patient refund password for PatientId: {PatientId}", request.PatientId);
                    return new PatientRefundPasswordUpdateResponse
                    {
                        Success = false,
                        Message = "更新失败，请稍后重试",
                        PatientId = request.PatientId
                    };
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error occurred while updating patient refund password for PatientId: {PatientId}", request.PatientId);
                return new PatientRefundPasswordUpdateResponse
                {
                    Success = false,
                    Message = $"更新过程中发生错误: {ex.Message}",
                    PatientId = request.PatientId
                };
            }
        }
    }
}